package com.glinsoft.ticktak.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import com.glinsoft.ticktak.entity.Task;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

import org.springframework.beans.BeanUtils;

import java.io.Serializable;

import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.List;


/**
 * task
 */
@SuppressWarnings("serial")
@ApiModel("任务")
@Data
public class TaskVo implements Serializable {
    /** 主键； id **/
    @ApiModelProperty(value = "主键")
    public String id;

    /** cron表达式； cron **/
    @ApiModelProperty(value = "cron表达式")
    public String cron;

    /** 任务类型; 1:http,2:feign,3:stub-event;4:rpc-local； type **/
    @ApiModelProperty(value = "任务类型; 1:http,2:feign,3:stub-event;4:rpc-local")
    public Integer type;

    /** 目标服务； target_service **/
    @ApiModelProperty(value = "目标服务")
    public String targetService;

    /** 目标类名； target_class **/
    @ApiModelProperty(value = "目标类名")
    public String targetClass;

    /** 目标方法名； target_method **/
    @ApiModelProperty(value = "目标方法名")
    public String targetMethod;

    /** 参数; JSON格式参数； parameters **/
    @ApiModelProperty(value = "参数; JSON格式参数")
    public String parameters;

    /** 最近执行返回值； latest_returns **/
    @ApiModelProperty(value = "最近执行返回值")
    public String latestReturns;

    /** 创建时间； create_time **/
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    public LocalDateTime createTime;

    /** 任务状态; -1:terminated;0:paused;1:scheduling； status **/
    @ApiModelProperty(value = "任务状态; -1:terminated;0:paused;1:scheduling")
    public Integer status;

    /** 正在调度执行; 0:否,1:是； running **/
    @ApiModelProperty(value = "正在调度执行; 0:否,1:是")
    public Integer running;

    public static TaskVo fromPO(Task po) {
        if (po == null) {
            return null;
        }

        TaskVo vo = new TaskVo();
        BeanUtils.copyProperties(po, vo);

        return vo;
    }

    public static List<TaskVo> fromPO(List<Task> pos) {
        if ((pos == null) || (pos.size() == 0)) {
            return null;
        }

        List<TaskVo> r = new ArrayList();

        for (Task po : pos) {
            r.add(fromPO(po));
        }

        return r;
    }

    public Task toPO() {
        Task po = new Task();
        BeanUtils.copyProperties(this, po);

        return po;
    }

    public static List<Task> toPO(List<TaskVo> vos) {
        if ((vos == null) || (vos.size() == 0)) {
            return null;
        }

        List<Task> r = new ArrayList();

        for (TaskVo vo : vos) {
            r.add(vo.toPO());
        }

        return r;
    }
}
